home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 214_01 / xshowr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  9.0 KB  |  528 lines

  1. /* XSHOWR.C   VERS:- 01.00  DATE:- 09/26/86  TIME:- 09:37:51 PM */
  2. /*
  3. %CC1 $1.C -X -O -E5500
  4. %CLINK $1 XSHOWFNS WILDEXP -F CHARFUN STRFUN -S -E5500
  5. */
  6. /* 
  7. Description:
  8.  
  9. Display wildcard filelist, using wordstar-like commands:
  10.     move forward and back in a file, by line, screen or 2K block;
  11.     go to start or end of a file;
  12.     return to last position in file;
  13.     tab screen display back and forth;
  14.     search for a string (wildcards and control chars allowed);
  15.     single or continuous repeat of last command;
  16.     display filelist and select a file from it for display;
  17.     move one file forward or back in filelist.
  18.  
  19. See help_mess for instructions.
  20.  
  21. Output and input is through bios, to allow for special handling through
  22. functions b_getchar() and b_putchar().
  23.  
  24. Note: 
  25.     header file XSHOW.H
  26.     function file XSHOWFNS.C/CRL
  27.  
  28.  
  29. Speed tests:
  30.                 FCAS    XSRCH    XSHOW    Wordstar
  31.                   (machine    (C code)    
  32.                     code)        
  33. Search of 108K file 
  34. for "262 OF" in last block.    44"    85"    106"     20"
  35.  
  36. X-QQ-1 on 108K file                41"
  37. = time for read of each block
  38. of file, sequentially, into buffer
  39.  
  40. D-QQ-1 on 108K file                131"
  41. = rate of read and display of characters    800 char/second
  42.  
  43. XSHOW can be speeded up by ca 20% by use of in_line code instead of
  44. function calls to pntr_change() in find() and search(), to use_check()
  45. in display_file(), etc.
  46.  
  47.  
  48. The following functions are more or less extensive modifications 
  49. of code from public domain programs:
  50.     search(), find(), and decode():
  51.         adapted from Van Nuys SEARCH, by E. H. Mallory.
  52.     wildexp(): 
  53.         adapted from the Van Nuys and original BDS-C versions.
  54.  
  55.  
  56. By J.A. Rupley, Tucson, Arizona
  57. Coded for BDS C compiler, version 1.50a
  58. */
  59.  
  60.  
  61.     /* page eject */
  62.  
  63. #include <bdscio.h>
  64.  
  65. #include <xshow.h>
  66.  
  67.  
  68. main(argc, argv)
  69. int argc;
  70. char **argv;
  71. {
  72.     int y;
  73.     int string_compare();
  74.  
  75.     *pattern = *input_pattern = *old_pattern = '\0';
  76.     buffer[0] = '\0';
  77.     start_addr = &buffer[1];
  78.     mid_addr = start_addr + NSECS * SECSIZ;
  79.  
  80.     if (wildexp(&argc, &argv) == ERROR)
  81.         argc = 0;
  82.  
  83.     for (x = argc - 1; x > 0; x--)
  84.         if (bad_type(argv[x]))
  85.         {
  86.             for (y = x; y < (argc - 1); y++)
  87.                 argv[y] = argv[y + 1];
  88.             argc--;
  89.         }
  90.  
  91.     if (argc < 2)
  92.     {
  93.         help_mess();
  94.         exit();
  95.     }
  96.  
  97.     qsort(&argv[1], argc - 1, 2, &string_compare);
  98.  
  99.     for (x = 1; x < argc; x++)
  100.     {
  101.         setup(argv);
  102.         if (setjmp(jbuf))
  103.             error_reset();
  104.         while (display_file(argc, argv))
  105.             ;
  106.     }
  107. }
  108.  
  109.  
  110.         /*page eject*/
  111.  
  112. int display_file(argc, argv)
  113. int argc;
  114. char **argv;
  115.  
  116. {
  117.     int temp;
  118.     char *k_mess, *k_err_mess, *srch_mess;
  119.  
  120.     if ((disp_pntr != last_pntr) || (top_flag != last_flag) || (disp_sec != last_sec))
  121.     {
  122.         old_sec = last_sec;
  123.         last_sec = disp_sec;
  124.         old_flag = last_flag;
  125.         last_flag = top_flag;
  126.         old_pntr = last_pntr;
  127.         last_pntr = disp_pntr;
  128.     }
  129.  
  130.     if (!use_last && *file)
  131.     {
  132.         pause_mess();
  133.         if ((c1 = toupper(b_getchar())) < 0x20)
  134.             c1 += 0x40;
  135.     }
  136.     else
  137.         c1 = old_c1;
  138.  
  139.     switch (c1)
  140.     {
  141.         /* help */
  142.     case 'H' :
  143.         help_mess();
  144.         break;
  145.  
  146.         /* - line */
  147.     case 'S' :
  148.         gotop_display(0);
  149.         if (backup(1))
  150.         {
  151.             disp_pntr = start_addr;
  152.             disp_screen();
  153.             break;
  154.         }
  155.         CUR_TOP;
  156.         INSERT_LINE;
  157.         disp_skip(1);
  158.         CUR_BOTTOM;
  159.         ERA_EOL;
  160.         backup(1);
  161.         break;
  162.  
  163.         /* + line */
  164.     case 'D' :
  165.         gobot_display(0);
  166.         CUR_BOTTOM;
  167.         ERA_EOL;
  168.         disp_skip(1);
  169.         break;
  170.  
  171.         /* - screen */
  172.     case 'R' :
  173.         backup(TLENGTH);
  174.         gotop_display(1);
  175.         break;
  176.  
  177.         /* + screen */
  178.     case 'C' :
  179.         if (gobot_display(1))
  180.             gotop_display(1);
  181.         break;
  182.  
  183.         /* - block */
  184.     case 'E' :
  185.         down_block();
  186.         disp_screen();
  187.         break;
  188.  
  189.         /* + block */
  190.     case 'X' :
  191.         up_block();
  192.         disp_screen();
  193.         break;
  194.  
  195.         /* tab forward */
  196.     case 'F' :
  197.         tabcount += TAB_SPACES;
  198.         gotop_display(1);
  199.         break;
  200.  
  201.         /* tab back */
  202.     case 'A' :
  203.         if ((tabcount -= TAB_SPACES) < 0)
  204.             tabcount = 0;
  205.         gotop_display(1);
  206.         break;
  207.  
  208.         /* single repeat */
  209.     case 'L' :
  210.         use_last = 1;
  211.         return 1;
  212.  
  213.                  /*page eject*/
  214.  
  215.         /* K prefix */
  216.     case 'K' :
  217.         if (*file)
  218.             k_mess = ":::K-prefix mode: enter X,K,B,N,O,H....";
  219.         else
  220.             k_mess = ":::K-prefix mode with NO OPEN FILE: enter --ONLY-- X,N,O,H....";
  221.         if (!use_last)
  222.         {
  223.  
  224.             delay_mess(k_mess);
  225.             pause_mess();
  226.             if ((c2 = toupper(b_getchar())) < 0x20)
  227.                 c2 += 0x40;
  228.         }
  229.         else
  230.             c2 = old_c2;
  231.         k_err_mess = '\0';
  232.         switch (c2)
  233.         {
  234.             /* help */
  235.         case 'H' :
  236.             help_mess();
  237.             break;
  238.  
  239.             /* display file list */
  240.         case 'O' :
  241.             file_list(argc, argv);
  242.             break;
  243.  
  244.             /* exit */
  245.         case 'X' :
  246.             exit();
  247.  
  248.             /* open next file */
  249.         case 'K' :
  250.             if (*file && (argc > (x + 1)))
  251.                 return 0;
  252.             else
  253.                 k_err_mess = ":::No next file...";
  254.             break;
  255.  
  256.             /* open previous file */
  257.         case 'B' :
  258.             if (*file && (x > 1))
  259.             {
  260.                 x -= 2;
  261.                 return 0;
  262.             }
  263.             else
  264.                 k_err_mess = ":::No previous file...";
  265.             break;
  266.  
  267.             /* select file from file_list */
  268.         case 'N' :
  269.             delay_mess(":::Enter file_list ordinal (1 to %d) + -cr- terminator (current file = %d)...", argc - 1, x);
  270.             pause_mess();
  271.             n1 = atoi(gets(string1));
  272.             if (n1 && n1 < argc)
  273.             {
  274.                 x = n1 - 1;
  275.                 return 0;
  276.             }
  277.             else
  278.                 k_err_mess = ":::File ordinal out of range...";
  279.         }
  280.  
  281.         CUR_BOTTOM;
  282.         ERA_EOL;
  283.         printf(k_err_mess);
  284.         break;
  285.  
  286.                  /*page eject*/
  287.  
  288.         /* Q prefix */
  289.     case 'Q' :
  290.         if (!use_last)
  291.         {
  292.             delay_mess(":::Q-prefix mode: enter Q,P,R,C,X,E,F,H....");
  293.             pause_mess();
  294.             if ((c2 = toupper(b_getchar())) < 0x20)
  295.                 c2 += 0x40;
  296.         }
  297.         else
  298.             c2 = old_c2;
  299.         switch (c2)
  300.         {
  301.             /* help */
  302.         case 'H' :
  303.             help_mess();
  304.             break;
  305.  
  306.             /* search for string */
  307.         case 'F' :
  308.             if (!use_last)
  309.             {
  310.                 if (read_pattern())
  311.                     break;
  312.                 gotop_display(0);
  313.                 set_srch(FIRST, SRCH_CHAR);
  314.                 set_srch(NEXT, SRCH_CHAR);
  315.             }
  316.             else
  317.                 {
  318.                 set_srch(FIRST, NEXT);
  319.             }
  320.             CUR_BOTTOM;
  321.             ERA_EOL;
  322.             if (find() == FOUND)
  323.             {
  324.                 set_srch(SRCH_CHAR, FIRST);
  325.                 srch_display();
  326.                 srch_mess = ":::Search string FOUND...";
  327.             }
  328.             else
  329.             {
  330.                 set_srch(NEXT, SRCH_CHAR);
  331.                 if (disp_screen())
  332.                     gotop_display(1);
  333.                 srch_mess = ":::Search string NOTFOUND...";
  334.             }
  335.             CUR_UP;
  336.             ERA_EOL;
  337.             printf(srch_mess);
  338.             break;
  339.  
  340.             /* multiple repeats */
  341.         case 'Q' :
  342.             use_last = -1;
  343.             sleep_base = 2187;
  344.             return 1;
  345.  
  346.             /* return to previous file position */
  347.         case 'P' :
  348.             disp_sec = old_sec;
  349.             change_load(0);
  350.             disp_pntr = old_pntr;
  351.             top_flag = old_flag;
  352.             gotop_display(1);
  353.             break;
  354.  
  355.             /* start file */
  356.         case 'R' :
  357.             load_buffer();
  358.             disp_screen();
  359.             break;
  360.  
  361.             /* end file */
  362.         case 'C' :
  363.             change_load(end_sec / NSECS + 2);
  364.             disp_screen();
  365.             break;
  366.  
  367.             /* + or - n blocks */
  368.         case 'X' :
  369.         case 'E' :
  370.             if (!use_last)
  371.             {
  372.                 delay_mess(":::Enter # of blocks to be skipped + -cr- terminator...", argc - 1);
  373.                 pause_mess();
  374.                 n1 = atoi(gets(string1));
  375.             }
  376.             else
  377.                 n1 = old_n1;
  378.             if (!n1)
  379.                 break;
  380.             switch (c2)
  381.             {
  382.             case 'E' :
  383.                 down_block();
  384.                 temp = (1 - n1);
  385.                 break;
  386.  
  387.             case 'X' :
  388.                 up_block();
  389.                 temp = (n1 - 1);
  390.                 break;
  391.             }
  392.             if (temp)
  393.                 change_load(temp);
  394.             disp_screen();
  395.             break;
  396.         }
  397.  
  398.         break;
  399.  
  400.  
  401.         /*if no proper c1-switch character, then refresh display */
  402.     default :
  403.         gotop_display(1);
  404.         break;
  405.     }
  406.  
  407.              /* page eject */
  408.  
  409.     /* tests on exit from switches */
  410.     if (!use_last)
  411.     {
  412.         old_c1 = c1;
  413.         old_c2 = c2;
  414.         old_n1 = n1;
  415.     }
  416.     else
  417.         if (use_last > 0)
  418.         use_last--;
  419.     else
  420.         use_check();
  421.  
  422.     return 1;
  423. }
  424.  
  425.  
  426. void use_check()
  427. {
  428.     int sleep_temp;
  429.  
  430.     if (sleep_base)
  431.     {
  432.         sleep_temp = sleep_base;
  433.         while (sleep_temp--)
  434.             if (t || KBHIT)
  435.             {
  436.                 use_input();
  437.                 break;
  438.             }
  439.     }
  440.     if (!sleep_base)
  441.         use_input();
  442. }
  443.  
  444.  
  445. void use_input()
  446. {
  447.     int temp;
  448.  
  449.     if (isdigit(temp = b_getchar()))
  450.     {
  451.         if (temp -= '0')
  452.         {
  453.             sleep_base = 3;
  454.             while (--temp)
  455.                 sleep_base *= 3;
  456.         }
  457.         else
  458.             sleep_base = 0;
  459.     }
  460.     else
  461.         {
  462.         t = temp;
  463.         use_last = 0;
  464.     }
  465. }
  466.  
  467.  
  468.         /* page eject */
  469.  
  470. void setup(argv)
  471. char **argv;
  472. {
  473.     tabcount = 0;
  474.     old_pntr = last_pntr = disp_pntr;
  475.     old_flag = last_flag = top_flag;
  476.     old_sec = last_sec = disp_sec;
  477.     use_last = 0;
  478.     c1 = c2 = n1 = 0;
  479.     old_c1 = 'H';
  480.     *string1 = '\0';
  481.  
  482.     close(fd);
  483.     strcpy(file, argv[x]);
  484.     if ((fd = open(file, 0)) == ERROR)
  485.         error("Cannot open file.");
  486.     seek(fd, 0, 2);
  487.     end_sec = tell(fd);
  488.     load_buffer();
  489.     disp_screen();
  490. }
  491.  
  492.  
  493. void error_reset()
  494. {
  495.     old_c1 = 'K';
  496.     *file = '\0';
  497.     use_last = 0;
  498. }
  499.  
  500.  
  501. void file_list(argc, argv)
  502. int argc;
  503. char **argv;
  504. {
  505.     int i;
  506.  
  507.     CLEAR;
  508.     for (i = 1; i < argc; i++)
  509.     {
  510.         if ((i % 60) == 1)
  511.             printf("FILE_LIST:  Use ordinal number in <KS#> selection of file for display\n\n");
  512.         printf("%3d  %-20s", i, argv[i]);
  513.         if (!(i % 3))
  514.             printf("\n");
  515.         if (!(i % 60))
  516.         {
  517.             pause_mess();
  518.             if ((t = b_getchar()) == CR)
  519.                 t = 0;
  520.             else
  521.                 return;
  522.             CLEAR;
  523.         }
  524.     }
  525.     pause_mess();
  526. }
  527.  
  528.